package com.zygh.policeasset.controller;

import cn.hutool.core.util.StrUtil;
import com.zygh.policeasset.component.CommonResult;
import com.zygh.policeasset.mapper.ProjectMapper;
import com.zygh.policeasset.pojo.Project;
import com.zygh.policeasset.pojo.Tender;
import com.zygh.policeasset.service.TenderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping({"/tender"})
@Api(tags = {"招标管理模块"})
@RestController
/* loaded from: police-asset-2.0-0.0.1-SNAPSHOT.jar:BOOT-INF/classes/com/zygh/policeasset/controller/TenderController.class */
public class TenderController {
    @Autowired
    TenderService tenderService;
    @Autowired
    ProjectMapper projectMapper;

    @RequestMapping(value = {"/"}, method = {RequestMethod.GET})
    @ApiOperation("获取招标分页列表")
    public Object findAll(@RequestParam(required = false) String name,
                          @RequestParam(value = "status", required = false) Integer status,
                          @RequestParam(defaultValue = "1", value = "pageNum") int pageNum,
                          @RequestParam(defaultValue = "10", value = "pageSize") int pageSize,
                          @RequestParam(required = false) String time1,
                          @RequestParam(required = false) String time2,
                          @RequestParam(required = false) Integer category,
                          @RequestHeader("findAll") boolean findAll,
                          @RequestHeader("userId") Integer userId) {
        if (findAll) {
            return this.tenderService.findAll(name, status, pageNum, pageSize, time1, time2, category, null);
        }
        return this.tenderService.findAll(name, status, pageNum, pageSize, time1, time2, category, userId);
    }

    @RequestMapping(value = {"/status"}, method = {RequestMethod.GET})
    @ApiImplicitParams({@ApiImplicitParam(name = "status", value = "0未提交  2已提交", required = true, paramType = "query"), @ApiImplicitParam(name = "pageNum", value = "页码", required = true, paramType = "query"), @ApiImplicitParam(name = "pageSize", value = "页数", required = true, paramType = "query")})
    @ApiOperation("根据状态获取招标分页列表")
    public Object getByStatus(@RequestParam int status, @RequestParam(defaultValue = "1", value = "pageNum") int pageNum, @RequestParam(defaultValue = "10", value = "pageSize") int pageSize) {
        if (StrUtil.isEmpty(String.valueOf(status))) {
            return new CommonResult().validateFailed("状态不能为空");
        }
        return this.tenderService.findByStatus(status, pageNum, pageSize);
    }

    @RequestMapping(value = {"/list"}, method = {RequestMethod.GET})
    @ApiOperation("根据项目获取招标分页列表")
    public Object get(@RequestParam String projectNo, @RequestParam(defaultValue = "1", value = "pageNum") int pageNum, @RequestParam(defaultValue = "10", value = "pageSize") int pageSize) {
        if (StrUtil.isEmpty(projectNo)) {
            return new CommonResult().validateFailed("项目编号不能为空");
        }
        return this.tenderService.findByProject(projectNo, pageNum, pageSize);
    }

    @RequestMapping(value = {"/getadd"}, method = {RequestMethod.GET})
    @ApiOperation("获取可添加项目")
    public Object getAdd() {
        return this.tenderService.getAdd();
    }

    @RequestMapping(value = {"/add"}, method = {RequestMethod.POST})
    @ApiOperation("添加招标(废弃)")
    public Object add(@RequestBody Tender params) {
        if (StrUtil.isEmpty(params.getProjectNo())) {
            return new CommonResult().validateFailed("项目编号不能为空");
        }
        if (this.tenderService.add(params).intValue() > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("添加错误");
    }

    @RequestMapping(value = {"/status/update"}, method = {RequestMethod.POST})
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "编号", required = true, paramType = "query"), @ApiImplicitParam(name = "status", value = "状态", required = true, paramType = "query")})
    @ApiOperation("立项状态更新")
    public Object updateStatus(@RequestParam int id, @RequestParam int status) {
        if (StrUtil.isEmpty(String.valueOf(id))) {
            return new CommonResult().validateFailed("ID不能为空");
        }
        if (StrUtil.isEmpty(String.valueOf(status))) {
            return new CommonResult().validateFailed("ID不能为空");
        }
        if (this.tenderService.updateStatus(id, status).intValue() > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("更新错误");
    }

    @RequestMapping(value = {"/insert"}, method = {RequestMethod.POST})
    @ApiOperation("添加招标")
    public Object insert(@RequestBody Tender tender) throws ParseException {
        if (StrUtil.isEmpty(tender.getProjectNo())) {
            return new CommonResult().validateFailed("项目编号不能为空");
        }
        Project project = this.projectMapper.findByNo(tender.getProjectNo());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        if (sdf.parse(tender.getStartDatetime()).before(sdf.parse(project.getStartDatetime()))) {
            return new CommonResult().validateFailed("招标时间不能早立项时间");
        }
        if (StrUtil.isEmpty(tender.getProjectName())) {
            return new CommonResult().validateFailed("项目名称不能为空");
        }
        if (tender.getSectionList().size() == 0) {
            return new CommonResult().validateFailed("标段信息还未填写");
        }
        if (StrUtil.isEmpty(tender.getSectionList().get(0).getSectionName())) {
            return new CommonResult().validateFailed("标段信息还未填写");
        }
        for (int i = 0; i < tender.getSectionList().size(); i++) {
            if (StrUtil.isEmpty(tender.getSectionList().get(i).getSectionName())) {
                return new CommonResult().validateFailed("标段名称还未填写");
            }
        }
        if (this.tenderService.insert(tender).intValue() > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("添加错误");
    }

    @RequestMapping(value = {"/update"}, method = {RequestMethod.POST})
    @ApiOperation("修改招标")
    public Object update(@RequestBody Tender params) throws ParseException {
        if (StrUtil.isEmpty(String.valueOf(params.getId()))) {
            return new CommonResult().validateFailed("ID不能为空");
        }
        Project project = this.projectMapper.findByNo(params.getProjectNo());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        if (sdf.parse(params.getStartDatetime()).before(sdf.parse(project.getStartDatetime()))) {
            return new CommonResult().validateFailed("招标时间不能早立项时间");
        }
        if (params.getSectionList() == null) {
            return new CommonResult().validateFailed("标段信息还未填写");
        }
        if (StrUtil.isEmpty(params.getSectionList().get(0).getSectionName())) {
            return new CommonResult().validateFailed("标段信息还未填写");
        }
        for (int i = 0; i < params.getSectionList().size(); i++) {
            if (StrUtil.isEmpty(params.getSectionList().get(i).getSectionName())) {
                return new CommonResult().validateFailed("标段名称还未填写");
            }
        }
        if (this.tenderService.update(params).intValue() > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("修改错误");
    }

    @RequestMapping(value = {"/remove"}, method = {RequestMethod.GET})
    @ApiOperation("删除招标")
    public Object remove(@RequestParam int id) {
        if (StrUtil.isEmpty(String.valueOf(id))) {
            return new CommonResult().validateFailed("id不能为空");
        }
        if (this.tenderService.remove(id).intValue() > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("删除错误");
    }

    @RequestMapping(value = {"/findById"}, method = {RequestMethod.GET})
    @ApiOperation("按id查询招标信息")
    public Object findById(@RequestParam int id) {
        return new CommonResult().success(this.tenderService.findById(Integer.valueOf(id)));
    }

    @RequestMapping(value = {"/selectBytype"}, method = {RequestMethod.GET})
    @ApiOperation("查询所有招标通过的项目")
    public Object selectBytype() {
        return new CommonResult().success(this.tenderService.selectBytype());
    }
}